<template>
	<view>

		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">导航切换</block>
		</cu-custom>

		<!-- 利用 scroll-view和swiper制作顶部导航条: https://blog.csdn.net/ZkD953/article/details/82968200 -->
		<view class="body-view">

			<scroll-view class="top-menu-view" scroll-x="true" :scroll-left="scrollLeft">
				<block v-for="(menuTab,index) in menuTabs" :key="index">
					<view class="menu-one-view" v-bind:id="'tabNum'+index" @click="swichMenu(index)">
						<view :class="[currentTab==index ? 'menu-one-act' : 'menu-one']">
							<view class="menu-one-txt">{{menuTab.name}}</view>
							<view class="menu-one-bottom">
								<view :animation="animationData" class="menu-one-bottom-color"></view>
							</view>
						</view>
					</view>
				</block>
			</scroll-view>


			<swiper :current="currentTab" class="swiper-box-list" duration="300" @change="swiperChange">
				<block v-for="(swiperDate,index1) in menuTabs" :key="index1">
					<swiper-item>
						<scroll-view class="swiper-one-list" scroll-y="true" @scrolltolower="loadMore(index1)">
							
							<block v-for="(swiperDate2,index2) in 200" :key="index2">
									<view>数列：{{index2}}</view>
							</block>
							
						</scroll-view>
					</swiper-item>
				</block>
			</swiper>
					
		</view>

	</view>
</template>

<script>
	var that
	export default {

		data() {
			return {
				scrollLeft: 0,
				currentTab: 0,
				menuTabs: [{
					name: '页面1'
				}, {
					name: '页面2'
				}, {
					name: '页面3'
				},{
					name: '页面4'
				}, {
					name: '页面5'
				}, {
					name: '页面6'
				},{
					name: '页面7'
				}, {
					name: '页面8'
				}, {
					name: '页面9'
				}],
				swiperDateList: [],//数据
				animationData: {}//动画参数			
			}
		},

		onLoad: function() {
			//初始化数据
		},
		methods: {
			
			swichMenu: async function(current) { //点击其中一个 menu
				if (this.currentTab == current) {
					return false;
				} else {
					this.setScrollLeft(current);
					this.currentTab = current;
					console.log("点击其中一个 menu,改变第几页")
				}
			},
			
			swiperChange: async function(e) {
				let index = e.target.current;
				this.setScrollLeft(index);
				this.currentTab = index;
				console.log("改变第几页,切换其中一个 menu")
				
				//动画效果				
				var animation = uni.createAnimation({
				      duration: 100,
				      timingFunction: 'linear',
				 })
				this.animation = animation
				animation.translate(10).step()
				animation.translate(-10).step()
				animation.translate(0).step()
				this.animationData = animation.export()	
				 setTimeout(function() {
				      this.animationData = {}
				 }.bind(this), 200)
			},
			
			//----------------------------------超出屏幕滚动--------------------------------------
			
			setScrollLeft: async function(tabIndex) {
				let leftWidthSum = 150;
				for (var i = 0; i <= tabIndex; i++) {
					let nowElement = await this.getWidth('tabNum' + i);
					leftWidthSum = leftWidthSum + nowElement.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth;
				this.scrollLeft = leftWidthSum > winWidth ? (leftWidthSum - winWidth) : 0
			},
			getWidth: function(id) { //得到元素的宽高
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			
			//-------------------------------------------------------------------------
			
			loadMore: function(tabIndex) {
				console.log("下滑加载")
				this.getDateList(tabIndex);
			},
			getDateList: function(tabIndex) {
				console.log(tabIndex+1)
			}

		}

	}
</script>

<style lang="less">
	.body-view {
		/*全部内容高度*/
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		height: 100%;
		width: 100%;
		align-items: flex-start;
		justify-content: center;
	}
	.top-menu-view {
		/*导航条高度*/
		display: flex;
		white-space: nowrap;
		width: 100%;
		background-color: #FFFFFF;
		height: 110upx;	
		text-align: center;//导航条内容是否居中
	}

	.top-menu-view .menu-one-view {
		/*导航条内容*/
		display: inline-block;
		white-space: nowrap;
		height: 100%;
		padding-bottom: 10upx;
	}

	.top-menu-view .menu-one-view .menu-one {
		/* 未选中-公共样式  */
		margin:0 25upx;
		position: relative;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.top-menu-view .menu-one-view .menu-one .menu-one-txt {
		/* 未选中-字体样式 */
		height: 20upx;
		font-size: 28upx;
		font-weight: 400;
		color: rgba(154, 154, 154, 1);
		line-height: 40upx;
	}

	.top-menu-view .menu-one-view .menu-one .menu-one-bottom {
		/* 未选中-下标全  */
		position: absolute;
		bottom: 0;
		width: 100%;
	}

	.top-menu-view .menu-one-view .menu-one .menu-one-bottom .menu-one-bottom-color {
		/* 未选中-下标少  */
		width: 60%;
		height: 4upx;
	}

	.top-menu-view .menu-one-view .menu-one-act {
		/* 选中-公共样式  */
		margin:0 25upx;
		position: relative;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.top-menu-view .menu-one-view .menu-one-act .menu-one-txt {
		/* 选中-字体样式  */
		height: 20upx;
		font-size: 28upx;
		font-weight: 400;
		color: rgba(0, 170, 255, 1);
		line-height: 40upx;
	}

	.top-menu-view .menu-one-view .menu-one-act .menu-one-bottom {
		/* 选中-下标全  */
		position: absolute;
		bottom: 0;
		width: 100%;
		display: flex;
		justify-content: center;
	}

	.top-menu-view .menu-one-view .menu-one-act .menu-one-bottom .menu-one-bottom-color {
		/* 选中-下标少  */
		width: 60%;
		height: 4upx;
		background: rgba(0, 170, 255, 1);
	}

	.swiper-box-list {
		/* swiper高度=屏幕高度 */
		/*  #ifdef  APP-PLUS  */
			min-height: calc(100vh - 140px);
		/*  #endif  */
		/*  #ifdef  H5||MP  */
			min-height: calc(100vh - 200upx);
		/*  #endif  */
		width: 100%;
	}

	.swiper-one-list {
		/* swiper内容高度 */ 
		height: 100%;
		width: 100%;
	}

//list内容

</style>
